package fr.cephb.joperon.core.bio;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * A biological Sequence
 * contains methods to export to FASTA
 *
 */
public abstract class AbstractBiologicalSequence
implements BiologicalSequence
	{
	@Override
	public int hashCode()
		{
		final int prime = 31;
		int result = 1;
		for(int i=0;i< size();++i)
			{
			result = prime * result + at(i);
			}
		return result;
		}
	
	/** write this sequence to fasta */
	public void toFasta(PrintWriter out)
		{
		toFasta(out,0,size());
		}
	
	/** write this sequence to fasta */
	public void toFasta(PrintWriter out,int start,int end)
		{
		out.print(">"+getName());
		if(!(start==0 && end==size())) out.print("|("+start+"-"+end+")");
		for(int i=0;i< (end-start);++i)
			{
			if(i%70==0) out.println();
			out.print(at(start+i));
			}
		out.println();
		}
	
	/** write this sequence to fasta */
	public void toFasta(PrintStream out,int start,int end)
		{
		PrintWriter io= new PrintWriter(out);
		toFasta(io,start,end);
		io.flush();
		}
	
	/** write this sequence to fasta */
	public void toFasta(PrintStream out)
		{
		toFasta(out,0,size());
		}
	
	@Override
	public boolean equals(Object obj)
		{
		if(obj==this) return true;
		if(obj==null || !(obj instanceof BiologicalSequence)) return false;
		BiologicalSequence other=BiologicalSequence.class.cast(obj);
		if(other.size()!=this.size()) return false;
		for(int i=0;i< size();++i)
			{	
			if(other.at(i)!=at(i)) return false;
			}
		return true; 
		}
	
	
	@Override
	public String toString() {
		return getName()+"(size:"+size()+")";
		}	
}
